package com.cbtx.module_sp.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/6/10 5:39 PM
 * desc   :
 */
public class NetWorkUtil {


    /**
     * 检查网络是否可用
     *
     * @param paramContext
     * @return
     */
    public static boolean checkEnable(Context paramContext) {
        NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable())) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前网络状态
     *
     * @param context
     * @return
     */
    public static int getNetWorkState(Context context) {
        final int network_none = -1; // 无网络

        final int network_mobile = 0; // 移动网络

        final int network_wifi = 1; // 无线网络

        final int network_eth = 2; // 有线网络

        // 得到连接管理器对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return network_wifi;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return network_mobile;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_ETHERNET)) {
                return network_eth;
            }
        } else {
            return network_none;
        }
        return network_none;

    }

    public static String getIpAddress(int type) {
        String interfaceName = "";
        switch (type) {
            case -1:
                return "";
            case 0:
                return getDefaultIp();
            case 1:
                interfaceName = "wlan0";
                break;
            case 2:
                interfaceName = "eth0";
                break;
            default:
        }
        if (TextUtils.isEmpty(interfaceName)) {
            return "";
        }
        try {
            for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces();
                    enNetI.hasMoreElements();) {
                NetworkInterface netI = enNetI.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()
                            && TextUtils.equals(interfaceName, netI.getName())) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 随便取一个可用IP
     *
     * @return
     */
    public static String getDefaultIp() {
        try {
            for (Enumeration<NetworkInterface> enNetI
                    = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements();) {
                NetworkInterface netI = enNetI.nextElement();
                for (Enumeration<InetAddress> enumIpAddr
                        = netI.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当前连接网络的ip
     *
     * @return
     */
    public static String getLocalIp(Context context) {
        String ip = getIpAddress(getNetWorkState(context));
        if (TextUtils.isEmpty(ip)) {
            ip = getDefaultIp();
        }
        return ip;
    }
}

